package com.ysxsoft.wdg.ui.activity.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.order.ZyOrderDetailAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.cart.ShopcartBean;
import com.ysxsoft.wdg.bean.response.BackOrderResponse;
import com.ysxsoft.wdg.bean.response.CollectShopResponse;
import com.ysxsoft.wdg.constant.MCConfig;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.photopicker.activity.BGAPhotoPreviewActivity;
import cn.bingoogolapple.photopicker.widget.BGANinePhotoLayout;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import okhttp3.Call;

public class BackOrderDetailActivity extends BaseActivity {
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.title_tv_r)
    TextView titleTvR;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.orderNo)
    TextView orderNo;
    @BindView(R.id.orderTime)
    TextView orderTime;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.fs)
    TextView fs;
    @BindView(R.id.yy)
    TextView yy;
    @BindView(R.id.pdesc)
    TextView pdesc;
    @BindView(R.id.pping)
    TextView pping;
    @BindView(R.id.ninePhotoLayout)
    BGANinePhotoLayout ninePhotoLayout;
    @BindView(R.id.pmoney)
    TextView pmoney; @BindView(R.id.call)
    TextView call;
    @BindView(R.id.pnum)
    TextView pnum;
    @BindView(R.id.pshentime)
    TextView pshentime;
    @BindView(R.id.pno)
    TextView pno;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.content)
    TextView content;   @BindView(R.id.bottomLayout)
    LinearLayout bottomLayout;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;
    @BindView(R.id.parent)
    RelativeLayout parent;
    private String phone;
    private String orderId;
    ZyOrderDetailAdapter adapter;
    private boolean isStOrder;//true是实体订单 false不是
    private boolean isMjOrder;//true是满减订单 false不是

    public static void start(Context context, String orderId) {
        Intent intent = new Intent(context, BackOrderDetailActivity.class);
        intent.putExtra("orderId", orderId);
        context.startActivity(intent);
    }
    public static void start(Context context, String orderId,boolean isStOrder) {
        Intent intent = new Intent(context, BackOrderDetailActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("isStOrder", isStOrder);
        context.startActivity(intent);
    }
    public static void startByMj(Context context, String orderId) {
        Intent intent = new Intent(context, BackOrderDetailActivity.class);
        intent.putExtra("orderId", orderId);
        intent.putExtra("isMjOrder", true);
        context.startActivity(intent);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_back_order_detail;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("退款详情");
        orderId = getIntent().getStringExtra("orderId");
        isStOrder = getIntent().getBooleanExtra("isStOrder",false);
        isMjOrder = getIntent().getBooleanExtra("isMjOrder",false);

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setFocusable(false);
        adapter = new ZyOrderDetailAdapter(R.layout.include_order_goods);
        recyclerview.setAdapter(adapter);

        getDetail(orderId);
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打电话
                if (TextUtils.isEmpty(phone)) {
                    ToastUtils.showToast("请稍后再试");
                    return;
                }
                ViewUtils.call(BackOrderDetailActivity.this, phone);
            }
        });
    }

    @Override
    protected void setListener() {

    }

    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取订单详情
     */
    private void getDetail(String orderId) {
        OkHttpUtils.post()
                .url(UrlHost.BACK_ORDER_DETAIL)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("orderid", orderId)//订单id
                .addParams("style", isMjOrder?"3":isStOrder?"2":"1")//1自营满减2实体 3满减
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    BackOrderResponse backOrderResponse = JsonUtils.parseByGson(response, BackOrderResponse.class);
                    if (backOrderResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(backOrderResponse.getCode())) {
                        BackOrderResponse.DataBean da = backOrderResponse.getData();
                        BackOrderResponse.DataBean.OrderBean order = da.getOrder();

                        phone=da.getMobile();//电话
                        if("0".equals(da.getZi())){// 0是联系商家   1联系客服
                            call.setText("联系商家");
                        }else{
                            call.setText("联系客服");
                        }
                        call.setVisibility(View.VISIBLE);

                        int sta=order.getType();
                        String t="";
                        if(sta==1){
                            t="仅退款 ";
                        }else if(sta==2){
                            t="退款退货 ";
                        }
                        fs.setText(t);

                        //0待审核 1已退款 2拒绝
                        if(order.getStatus()==0){
                            status.setText(t+"待审核");
                            bottomLayout.setVisibility(View.GONE);
                            view1.setVisibility(View.GONE);
                        }else if(order.getStatus()==1){
                            status.setText(t+"已退款");
                            bottomLayout.setVisibility(View.GONE);
                            view1.setVisibility(View.GONE);
                        }else if(order.getStatus()==2){
                            status.setText(t+"审核不通过");
                            view1.setVisibility(View.VISIBLE);
                            bottomLayout.setVisibility(View.VISIBLE);
                        }

                        orderNo.setText(order.getDsn() + "");
                        orderTime.setText(order.getAddtime());
                        adapter.setNewData(da.getDetail());//退款详情
                        tvTotalMoney.setText("￥" + order.getAccount() + "");//总价格
                        yy.setText(order.getReason());//原因
                        pdesc.setText(order.getRemark());//备注

                        ArrayList<String> pics=new ArrayList<>();
                        pics.addAll(order.getPic());
                        ninePhotoLayout.setData(pics);
                        ninePhotoLayout.setDelegate(new BGANinePhotoLayout.Delegate() {
                            @Override
                            public void onClickNinePhotoItem(BGANinePhotoLayout ninePhotoLayout, View view, int position, String model, List<String> models) {
                                File downloadDir = new File(MCConfig.PICTURE_DIR);
                                BGAPhotoPreviewActivity.IntentBuilder photoPreviewIntentBuilder = new BGAPhotoPreviewActivity.IntentBuilder(mContext)
                                        .saveImgDir(downloadDir); // 保存图片的目录，如果传 null，则没有保存图片功能
                                photoPreviewIntentBuilder
                                        .previewPhotos((ArrayList<String>) models).currentPosition(position); // 当前预览图片的索引
                                mContext.startActivity(photoPreviewIntentBuilder.build());
                            }
                        });

                        pmoney.setText("￥"+order.getAccount()+"");
                        pnum.setText(order.getYun()+"");
                        pshentime.setText(order.getAddtime());
                        pno.setText(order.getBack_dsn());
                        content.setText(order.getRefuse());
                    } else {
                        ToastUtils.showToast(backOrderResponse.getMs());
                    }
                } else {
                }
            }
        });
    }

    class ZyOrderDetailAdapter extends BaseQuickAdapter<BackOrderResponse.DataBean.DetailBean, BaseViewHolder> {
        public ZyOrderDetailAdapter(int layoutResId) {
            super(layoutResId);
        }

        public void clear() {
            getData().clear();
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, BackOrderResponse.DataBean.DetailBean item) {
            ViewUtils.loadImage(mContext, item.getPic(), (ImageView) helper.getView(R.id.iv_goods));
            helper.setText(R.id.tv_goods_name, item.getProduct_name());
            helper.setText(R.id.tv_rules, item.getRule_name());
            helper.setText(R.id.tv_money, mContext.getString(R.string.price_rmb, item.getAccount()));
            helper.setText(R.id.tv_num, "x" + item.getNum());
        }
    }

}
